package com.ad.backstage.common.config;

import com.ad.backstage.common.interceptor.RequestHolder;
import com.ad.payload.model.CurrentUser;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author:dxgong
 * @日期 2021/3/8 16:57
 * @注释 mp公用字段自动填充器
 */
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        CurrentUser currentUser = RequestHolder.getCurrentUser();
        if(currentUser != null){
            //this.setFieldValByName("createUserId", currentUser.getId(),metaObject);
            // this.setFieldValByName("createUserName", currentUser.getWeChatName(),metaObject);
            this.setFieldValByName("createTime", new Date(),metaObject);
            //this.setFieldValByName("updateUserId", currentUser.getId(),metaObject);
            //this.setFieldValByName("updateUserName", currentUser.getWeChatName(),metaObject);
            this.setFieldValByName("updateTime", new Date(),metaObject);
        } else {
            //this.setFieldValByName("createUserId", "1", metaObject);
            //this.setFieldValByName("createUserName", "admin_", metaObject);
            this.setFieldValByName("createTime", new Date(),metaObject);
            //this.setFieldValByName("updateUserId", "1",metaObject);
            //this.setFieldValByName("updateUserName", "admin_",metaObject);
            this.setFieldValByName("updateTime", new Date(),metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        CurrentUser currentUser = RequestHolder.getCurrentUser();
        if(currentUser != null){
            //this.setFieldValByName("updateUserId", currentUser.getId(),metaObject);
            //this.setFieldValByName("updateUserName", currentUser.getWeChatName(),metaObject);
            this.setFieldValByName("updateTime", new Date(),metaObject);
        } else {
            //this.setFieldValByName("updateUserId", "1",metaObject);
            //this.setFieldValByName("updateUserName", "admin_",metaObject);
            this.setFieldValByName("updateTime", new Date(),metaObject);
        }

    }
}
